package part1.api.collection;

import java.util.*;

public class Test2 {
    public static void main(String[] args) {

        Collection c1 = new ArrayList();
        //add()方法
        c1.add("one");
        c1.add("two");
        c1.add("three");
        c1.add("four");
        c1.add("two");
        System.out.println(c1);

        //size()方法
        System.out.println(c1.size());

        //isEmpty() 判断集合是否为空
        boolean b1 = c1.isEmpty();
        System.out.println(b1);

        //clear()清空集合
        //c1.clear();

        //判断集合是否包含某个元素 equals比较
        boolean b2 = c1.contains("one");
        System.out.println(b2);

        //删除集合某个元素 若有重复只删除第1个
        boolean b3 = c1.remove("two");
        System.out.println(c1);

        Collection c2 = new ArrayList();
        c2.add("four");
        c2.add("five");
        c2.add("six");
        c2.add("seven");

        //将集合c2添加到c1中
        boolean b4 = c1.addAll(c2);
        System.out.println(c1);

        //判断集合c1是否包含c2的全部元素 equals比较
        boolean b5 = c1.contains(c2);
        System.out.println(b5);

        //取c1和c2的交集 c1直接变成交集元素
        boolean b6 = c1.retainAll(c2);
        System.out.println(c1);

        //删除c1和c2交集 c1直接删除交集
        boolean b7 = c1.removeAll(c2);
        System.out.println(c1);

        //获取迭代器接口方法
        Iterator it = c2.iterator();
        while (it.hasNext()){
            String str = (String) it.next();
            System.out.println(str);
        }

        //将集合转换为数组
        Object[] strs = c2.toArray();
        System.out.println(Arrays.toString(strs));

        System.out.println("---------------");

        List<String> list = new ArrayList<>();
        list.add("tom");
        list.add("bob");
        list.add("anni");
        list.add("keven");

        //集合排序 sort()
        Collections.sort(list);
        System.out.println(list);


        List<Student1> list1 = new ArrayList<>();
        list1.add(new Student1("tom",24));
        list1.add(new Student1("bob",32));
        list1.add(new Student1("anin",32));
        list1.add(new Student1("peter",24));
        Collections.reverse(list1);
        System.out.println(list1);

        //集合排序2 集合元素未实现comparable接口
        //按年龄升序 若年龄相等则按名字自然排序
        Collections.sort(list1,(o1,o2)->{
            int tem = o1.getAge()-o2.getAge();
            return tem==0 ? o1.getName().compareTo(o2.getName()) : tem;
        });
        System.out.println(list1);

        Set<Student1> set = new TreeSet<>();
        set.add(new Student1("anni",23));


    }
}

class Student1{
    private String name;
    private Integer age;

    public Student1() {
    }

    public Student1(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


}
